package com.shujia.kafka;

import org.apache.kafka.clients.producer.KafkaProducer;
import org.apache.kafka.clients.producer.ProducerRecord;

import java.util.Properties;

public class Demo01KafkaProducer {
    public static void main(String[] args) {
        Properties properties = new Properties();
        // Kafka集群的地址
        properties.setProperty("bootstrap.servers", "master:9092,node1:9092,node2:9092");
        // 设置Key及Value的序列化方式
        properties.setProperty("key.serializer", "org.apache.kafka.common.serialization.StringSerializer");
        properties.setProperty("value.serializer", "org.apache.kafka.common.serialization.StringSerializer");
        KafkaProducer<String, String> producer = new KafkaProducer<>(properties);

        // topic如果不存在 会自己创建
        producer.send(new ProducerRecord<String,String>("t1","hello"));
        producer.send(new ProducerRecord<String,String>("t1","hello"));
        producer.send(new ProducerRecord<String,String>("t1","hello"));
        producer.send(new ProducerRecord<String,String>("t1","hello"));

        // 将数据从缓冲区刷写到磁盘
        producer.flush();
    }
}
